package com.gjy.poi.use;

import com.gjy.poi.entity.HW;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 宫静雨
 * @version v1.0
 * @since 2022-11-16 19:23:28
 */
public class PoiTest {

    // Invalid row number (1048576) outside allowable range (0..1048575)
    private static final Integer SUM = 1040000;

    public static void main(String[] args) throws IllegalAccessException, IOException {
//        XSSFWorkbook workbook = new XSSFWorkbook();
        SXSSFWorkbook workbook = new SXSSFWorkbook();
//        XSSFSheet sheet = workbook.createSheet("sheet1");
        SXSSFSheet sheet = workbook.createSheet("sheet1");
        for (int i = 0; i < SUM; i++) {
//            XSSFRow row = sheet.createRow(i);
            SXSSFRow row = sheet.createRow(i);
            otr(list.get(i), row);
        }
        FileOutputStream fos = new FileOutputStream("sheet1.xlsx");
        workbook.write(fos);
    }

    private static void otr(HW hw, Row row) throws IllegalAccessException {
        Field[] fields = hw.getClass().getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            fields[i].setAccessible(true);
            Cell cell = row.createCell(i);
            Object o = fields[i].get(hw);
            if (o instanceof String) {
                cell.setCellValue(((String) o));
            } else if (o instanceof LocalDateTime) {
                cell.setCellValue(((LocalDateTime) o));
            } else if (o instanceof BigDecimal) {
                cell.setCellValue(((BigDecimal) o).doubleValue());
            } else if (o instanceof Boolean) {
                cell.setCellValue(((Boolean) o));
            } else if (o instanceof Integer) {
                cell.setCellValue(((Integer) o));
            }
        }
    }

    private static void otr(HW hw, XSSFRow row) throws IllegalAccessException {
        Field[] fields = hw.getClass().getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            fields[i].setAccessible(true);
            XSSFCell cell = row.createCell(i);
            Object o = fields[i].get(hw);
            if (o instanceof String) {
                cell.setCellValue(((String) o));
            } else if (o instanceof LocalDateTime) {
                cell.setCellValue(((LocalDateTime) o));
            } else if (o instanceof BigDecimal) {
                cell.setCellValue(((BigDecimal) o).doubleValue());
            } else if (o instanceof Boolean) {
                cell.setCellValue(((Boolean) o));
            } else if (o instanceof Integer) {
                cell.setCellValue(((Integer) o));
            }
        }
    }

    private static final List<HW> list = new ArrayList<>(SUM);

    static {
        HW hw = new HW();
        hw.setId(100000);
        hw.setName("HW");
        hw.setK5("k5");
        hw.setN3(LocalDateTime.now());
        hw.setB3(BigDecimal.valueOf(1000));
        for (int i = 0; i < SUM; i++) {
            list.add(hw);
        }
    }
}
